package org.redisson;



import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import org.redisson.api.RFuture;
import org.redisson.api.RTopic;
import org.redisson.api.listener.BaseStatusListener;
import org.redisson.api.listener.MessageListener;
import org.redisson.connection.ConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import io.netty.util.concurrent.FutureListener;

/**
 * description：QueueTransferTask
 *
 * @author duan
 * @date 2024/12/5 15:33
 */
public abstract class QueueTransferTask {

    private static final Logger log = LoggerFactory.getLogger(QueueTransferTask.class);

    public static class TimeoutTask {

        private final long startTime;
        private final Timeout task;

        public TimeoutTask(long startTime, Timeout task) {
            super();
            this.startTime = startTime;
            this.task = task;
        }

        public long getStartTime() {
            return startTime;
        }

        public Timeout getTask() {
            return task;
        }

    }

    private int usage = 1;
    private final AtomicReference<TimeoutTask> lastTimeout = new AtomicReference<TimeoutTask>();
    private final ConnectionManager connectionManager;

    public QueueTransferTask(ConnectionManager connectionManager) {
        super();
        this.connectionManager = connectionManager;
    }

    public void incUsage() {
        usage++;
    }

    public int decUsage() {
        usage--;
        return usage;
    }

    private int messageListenerId;
    private int statusListenerId;

    public void start() {
        RTopic<Long> schedulerTopic = getTopic();
        statusListenerId = schedulerTopic.addListener(new BaseStatusListener() {
            @Override
            public void onSubscribe(String channel) {
                pushTask();
            }
        });

        messageListenerId = schedulerTopic.addListener(new MessageListener<Long>() {
            @Override
            public void onMessage(String channel, Long startTime) {
                scheduleTask(startTime);
            }
        });
    }

    public void stop() {
        RTopic<Long> schedulerTopic = getTopic();
        schedulerTopic.removeListener(messageListenerId);
        schedulerTopic.removeListener(statusListenerId);
    }

    private void scheduleTask(final Long startTime) {
        System.out.println("name = " + Thread.currentThread().getName() + ", id = " + Thread.currentThread().getId());
        TimeoutTask oldTimeout = lastTimeout.get();
        if (startTime == null || (oldTimeout != null && oldTimeout.getStartTime() < startTime)) {
            return;
        }

        if (oldTimeout != null) {
            oldTimeout.getTask().cancel();
        }

        long delay = startTime - System.currentTimeMillis();
        if (delay > 10) {
            Timeout timeout = connectionManager.newTimeout(new TimerTask() {
                @Override
                public void run(Timeout timeout) throws Exception {
                    pushTask();
                    System.out.println("run name = " + Thread.currentThread().getName() + ", run id = " + Thread.currentThread().getId());

                    TimeoutTask currentTimeout = lastTimeout.get();
                    if (currentTimeout.getTask() == timeout) {
                        lastTimeout.compareAndSet(currentTimeout, null);
                    }
                }
            }, delay, TimeUnit.MILLISECONDS);
            if (!lastTimeout.compareAndSet(oldTimeout, new TimeoutTask(startTime, timeout))) {
                timeout.cancel();
            }
        } else {
            pushTask();
        }
    }

    protected abstract RTopic<Long> getTopic();

    protected abstract RFuture<Long> pushTaskAsync();

    private void pushTask() {
        RFuture<Long> startTimeFuture = pushTaskAsync();
        startTimeFuture.addListener(new FutureListener<Long>() {
            @Override
            public void operationComplete(io.netty.util.concurrent.Future<Long> future) throws Exception {
                if (!future.isSuccess()) {
                    if (future.cause() instanceof RedissonShutdownException) {
                        return;
                    }
                    log.error(future.cause().getMessage(), future.cause());
                    scheduleTask(System.currentTimeMillis() + 5 * 1000L);
                    return;
                }

                if (future.getNow() != null) {
                    scheduleTask(future.getNow());
                }
            }
        });
    }

}

